<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;
use app\common\library\EasyWechat;
use think\cache\driver\Redis;
use think\facade\Config;
use think\facade\Cache;
use app\api\common\controller\LuckDraw;



/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Demo extends Api
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    // {__CONTENT__}
    /** function demoList1()
     * @Apidoc\Title("列表")
     * @Apidoc\Desc("列表接口")
     * @Apidoc\Url("/api/Demo/demoList1")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("tag")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param(ref="pagingParam")
     * @Apidoc\Param("name",type="string",require="1",desc="1")
     * @Apidoc\Returned(ref="pagingParam")
     *
     * @Apidoc\Returned("data",type="array",ref="\app\api\model\User\getFieldsDesc")

     */

    /*
    测试支付
     */
    public function testPay(){
        $a = createSerialNumber(1);
        halt($a);
    }
    public function testShow(){
        
        
        halt(1);

        return show(config("status.action_not_found"), "找不到该方法", null, 404);
    }
    
    
    public function testRedis(){
        $redis = Cache::store('redis');
        $redis->set('name',6);
        $value = $redis->get('name');
        echo $value;
        //$redis = new Redis();

        // try{
        //     $redis->connect("172.17.0.5",6379);
        // }catch(Exception $e){
        //     var_dump($e->getMessage());
        // }
        //$redis->get("key");
    }
    public function testCode(){
        $redis = Cache::store('redis');
        $key = Config::get('token.key');
        $str = Config::get('token.str');
        $token = authcode($str,'ENCODE',$key,0); //加密 
        $user_id = Config::get('token.memberprefix')."zyl";
        $expire = Config::get('token.expire');
        //halt(Config::get('token'));
        $redis->set($user_id,$token,$expire);
        //halt(authcode($token,'DECODE',$key,0));
        halt($token);

        Token::connect(Config::get('token'));
        $key = Config::get('token.key');
        $str = Config::get('token.str');
        $token = authcode($str,'ENCODE',$key,0); //加密 
        $user_id = Config::get('token.prefix')."zyl";
        // halt($user_id);
        Token::set($token,$user_id);

        halt($token);
        
        
        $key = Config::get('token.key');
        $str = Config::get('token.str');
        $authcode =  authcode($str,'ENCODE',$key,0); //加密 
        echo $authcode."<br>";
        echo authcode($authcode,'DECODE',$key,0); //解密 
    }
    public function testToken(){
        $token = "b289+4BbkY9l/TMLzot11s5sbnRBrtr0OzVSiapTUB+8fA6caIiem7hcH3F6t3Q";
        $decodeToken = authcode($token,'DECODE',config('token.key'),0);
        halt($decodeToken);

        
        /*$key = config('token.memberprefix')."-".$this->user_member_id;

        $authcode = authcode($token,'DECODE',$key,0); //解密
        halt($authcode);*/
        /*$a =[];
        for($i=0;$i<50;$i++){
            array_push($a,getUserInviteCode());
            //echo getUserInviteCode()."<br>";
        }
        halt(array_unique($a));
        die;*/
        $tmpId = "456";
        $str = 'zhaoyongli'.'||'.$tmpId; 
        $key = 'www.fyunw.com'; 
        $authcode =  authcode($str,'ENCODE',$key,0); //加密

        echo $authcode."<br>";
        $authcode = authcode($authcode,'DECODE',$key,0); //解密 
        $arr = explode('||',$authcode);
        if($arr[0] == 'zhaoyongli'){
            halt('ok');
        }
 
    }
    public function demoList1()
    {
        $result = $this->validate(
            [
                'name' => input('name'),

            ],
            [
                'name' => 'require',

            ],
            [
                'name' => '名称不能为空',

            ]
        );
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $result =
            [

        ];

        try {
            $res = \app\api\model\User::where(['name' => input('name')])->paginate();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('ok', $res);
    } // function demoList1()







	/** function add()
	    * @Apidoc\Title("添加")
	    * @Apidoc\Desc("添加接口")
	    * @Apidoc\Url("/api/Demo/add")
	    * @Apidoc\Method("POST")
	    * @Apidoc\Tag("tag")
	    * @Apidoc\Header(ref="auth")
	    * 
	    * @Apidoc\Param("name",type="string",require="1",desc="1")
	    * 
	    * 
	    * 
	    
	*/
	public function add()
	{
	    $result = $this->validate(
            [
                'name'=>input('name'),
			
            ],
            [
                'name'=>'require',
			
            ],
            [
                'name'=>'名称不能为空',
			
            ]
        );
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
                        
		$result =
        [
            'name'=>input('name'),
			
        ];
            
        try {
            $res = \app\api\model\User::insert($result);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        
        $this->success('ok',$res);
	} // function add()

	/** function edit()
	    * @Apidoc\Title("添加")
	    * @Apidoc\Desc("添加接口")
	    * @Apidoc\Url("/api/Demo/edit")
	    * @Apidoc\Method("POST")
	    * @Apidoc\Tag("tag")
	    * @Apidoc\Header(ref="auth")
	    * 
	    * @Apidoc\Param("name",type="string",require="1",desc="1")
	    * 
	    * 
	    * @Apidoc\Returned("data",type="array",ref="\app\api\model\User\getFieldsDesc")
	    
	*/
	public function edit()
	{
	    $result = $this->validate(
            [
                'name'=>input('name'),
			
            ],
            [
                'name'=>'require',
			
            ],
            [
                'name'=>'名称不能为空',
			
            ]
        );
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
                        
		$result =
        [
            'name'=>input('name'),
			
        ];
            
        try {
            $res = \app\api\model\User::where(['name'=>input('name'),])->find();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        
        $this->success('ok',$res);
	} // function edit()

    function getRealIP(){
        $forwarded = request()->header("x-forwarded-for");
        if($forwarded){
            $ip = explode(',',$forwarded)[0];
        }else{
            $ip = request()->ip();
        }
        return $ip;
    }
    /*public function taobaoIP($clientIP){
        $taobaoIP = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$clientIP;
        $IPinfo = json_decode(file_get_contents($taobaoIP));
        $province = $IPinfo->data->region;
        $city = $IPinfo->data->city;
        $data = $province.$city;
        return $data;
    }*/
    public function getIp(){
        $headers = array();
        array_push($headers, "X-Bce-Signature:AppCode/" . "071c6c8ba3dd48eda4d0e4a7a0b8b24c");
        array_push($headers, "Content-Type".":"."application/json;charset=UTF-8");
        

        
        $url = "https://ipaddquery.api.bdymkt.com"."/ip/query"."?ip=".request()->ip();

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        
        halt(curl_exec($curl));
        /*$ip = get_client_ip(); //获取当前用户的ip  
        $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";  
        $data = file_get_contents($url); //调用淘宝接口获取信息  
        echo $data; */ 
        /*$ip = request()->ip();
        halt($ip);
        $ch = curl_init("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}");
        curl_setopt($ch,CURLOPT_ENCODING ,'utf8'); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回 
        $location = json_decode(curl_exec($ch));
        curl_close($ch);
        halt($location);
        if ($location->ret == -1) {
            return [
                'status' => 0,
                'info' => '获取地理位置失败'
            ];
        }
        return [
            'status' => 1,
            'info' => '获取地理位置成功',
            'handle' => $location
        ];*/
       
        /*$res = restRequest('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json','get',request()->ip());
        $res = json_decode($res,true);
        halt($res);*/
    }

    public function award(){
        halt(LuckDraw::res());
    }


}